package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.pojo.dto.UserOrderDTO;
import cn.icanci.ladybird.pojo.dto.UserSeatSelectionDTO;
import cn.icanci.ladybird.pojo.entity.*;
import cn.icanci.ladybird.service.*;
import cn.icanci.ladybird.service.user.UserSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: YPF
 * @date: Created in 2020/10/2 20:35
 * @classAction:
 */
@Service
public class UserSeatServiceImpl implements UserSeatService {
    @Autowired
    TicketPriceService ticketPriceService;
    @Autowired
    TicketSeatService ticketSeatService;
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    TicketSeatTicketPriceMiddleService ticketSeatTicketPriceMiddleService;
    @Autowired
    UserInfoOrderInfoMiddleService userInfoOrderInfoMiddleService;
    @Override
    public List<UserSeatSelectionDTO> userSeatSelectionDto(Long ticketId) {
        //需要封装的集合
        ArrayList<UserSeatSelectionDTO> userSeatSelectionDTOS = new ArrayList<>();
        List<TicketSeat> ticketSeatList2 = ticketSeatService.queryManyByTicketDetailsId(ticketId);
        //遍历此票id所有座位
        for (TicketSeat ticketSeat : ticketSeatList2) {
            //创建DTO对象
            UserSeatSelectionDTO userSeatSelectionDTO = new UserSeatSelectionDTO();
            userSeatSelectionDTO.setTicketSeatId(ticketSeat.getTicketSeatId());
            userSeatSelectionDTO.setTicketX(ticketSeat.getTicketX());
            userSeatSelectionDTO.setTicketY(ticketSeat.getTicketY());
            userSeatSelectionDTO.setTicketEnable(ticketSeat.getTicketEnable());
            userSeatSelectionDTO.setTicketState(ticketSeat.getTicketState());
            //查询中间表对象
            //查询的是价格
            List<TicketSeatTicketPriceMiddle> ticketSeatTicketPriceMiddles = ticketSeatTicketPriceMiddleService.queryManyById(ticketSeat.getTicketSeatId());
            //遍历中间表
            for (TicketSeatTicketPriceMiddle middle : ticketSeatTicketPriceMiddles) {
                Long ticketPriceMiddleId = middle.getTicketPriceMiddleId();
                TicketPrice ticketPrice = ticketPriceService.queryById(ticketPriceMiddleId);
                userSeatSelectionDTO.setTicketPricePrice(ticketPrice.getTicketPricePrice());
                break;
            }

            userSeatSelectionDTOS.add(userSeatSelectionDTO);
        }
        return userSeatSelectionDTOS;
    }

    @Override
    public UserOrderDTO userCreatOrderDto(Long userInfoId, String ticketDetailsInfo, Date ticketTime, Long ticketDetailsInfoId,
                                          Double orderInfoTotalPrice, String ticketAddressVenue) {
        UserOrderDTO userOrderDTO = new UserOrderDTO();
        //插入订单表
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderInfoTicketDetailsId(ticketDetailsInfoId);
        orderInfo.setOrderInfoTotalPrice(orderInfoTotalPrice);
        orderInfo.setCreateTime(new Date());
        orderInfo.setOrderInfoTime(new Date());
        orderInfo.setUpdateTime(new Date());
        orderInfo.setOrderInfoStatus(1);
        orderInfo.setTicketTime(ticketTime);
        orderInfo.setTicketDetailsInfo(ticketDetailsInfo);
        orderInfo.setTicketAddressVenue(ticketAddressVenue);
        orderInfo.setOrderNumber(100000L);
        Long orderInfoId = orderInfo.getOrderInfoId();
        orderInfoService.insert(orderInfo);
        //插入中间表
        UserInfoOrderInfoMiddle userInfoOrderInfoMiddle = new UserInfoOrderInfoMiddle();
        userInfoOrderInfoMiddle.setUserInfoMiddleId(userInfoId);
        userInfoOrderInfoMiddle.setOrderInfoMiddleId(orderInfoId);
        userInfoOrderInfoMiddle.setCreateTime(new Date());
        userInfoOrderInfoMiddle.setUpdateTime(new Date());
        userInfoOrderInfoMiddleService.insert(userInfoOrderInfoMiddle);

        userOrderDTO.setUserInfoId(userInfoOrderInfoMiddle.getUserInfoMiddleId());
        userOrderDTO.setOrderNumber(orderInfo.getOrderNumber());
        userOrderDTO.setCreateTime(orderInfo.getCreateTime());
        userOrderDTO.setTicketDetailsInfo(orderInfo.getTicketDetailsInfo());
        userOrderDTO.setTicketTime(orderInfo.getTicketTime());
        userOrderDTO.setTicketAddressVenue(orderInfo.getTicketAddressVenue());
        userOrderDTO.setSeats(orderInfo.getSeats());
        userOrderDTO.setOrderInfoTotalPrice(orderInfo.getOrderInfoTotalPrice());
        userOrderDTO.setOrderInfoStatus(orderInfo.getOrderInfoStatus());

        return userOrderDTO;
    }
}
